using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using OpenComposite.EII.Controls.DataGridViewColumns;
using System.Diagnostics;
using OpenComposite.EII.Controls;
using System.IO;
using OpenComposite.EII.Designer.Workflow;

namespace OpenComposite.EII.Forms
{
	public partial class FormBindingMapping : DockContent
	{
		#region Initialization
		public FormBindingMapping(WorkflowController wfcontroller)
		{
			if ( wfcontroller == null )
				throw new ArgumentNullException("wfcontroller", "Missing workflow controller.");
			
			_wfcontroller = wfcontroller;
			_workflow = wfcontroller.Workflow;

			InitializeComponent();

			init();
		}

		private void init()
		{
		}
		#endregion

		#region Public Members

		#region Properties
		#endregion

		#endregion

		#region Private Members

		#region Fields
		private Repository.Workflow _workflow;
		private WorkflowController _wfcontroller;
		#endregion

		#region Methods
		private void displayWorkflowInfos()
		{
			txtWorkflow.Text = _workflow.Name;
			if ( _workflow.Type == OpenComposite.EII.Repository.WorkflowType.Business_Process ) {
				// workflow is business process
				tsbtnDeploy.Text = "Deploy / Update Business Process";
				tsbtnRedeploy.Text = "Redeploy Business Process";
			} else if ( _workflow.Type == OpenComposite.EII.Repository.WorkflowType.Mashup ) {
				// workflow is mashup
				tsbtnDeploy.Text = "Deploy / Update Mashup";
				tsbtnRedeploy.Text = "Redeploy Mashup";
			} else {
				// workflow is something else
				tsbtnDeploy.Text = "Deploy / Update Orchestration";
				tsbtnRedeploy.Text = "Redeploy Orchestration";
			}
		}
		private void fillLogicalCapabilityList()
		{
			this.SuspendLayout();
			RepositoryService repSvc = _workflow.GetService(typeof(RepositoryService)) as RepositoryService;
			Debug.Assert(repSvc != null);

			int[] logactids = _workflow.ExtendedProperties.LogicalActivityIDs;
			foreach ( int logactid in logactids ) {
				Repository.LogicalActivity la = repSvc.GetItem<Repository.LogicalActivity>(logactid);
				if ( la != null && !la.IsDeleted && ( la.VerbID > 0 || la.OperationID > 0 ) ) {
					addUCLogicalActivityBindingMapping(la);
				}
			}
			this.ResumeLayout(true);
		}
		private void addUCLogicalActivityBindingMapping(Repository.LogicalActivity logAct)
		{
			UCLogicaActivityBindingMapping ctrl = new UCLogicaActivityBindingMapping(logAct, _workflow);
			ctrl.Width = pnlItems.Width - 20;
			ctrl.Anchor = AnchorStyles.Left | AnchorStyles.Top;
			pnlItems.Controls.Add(ctrl);
		}

		private void doDeploy(bool update)
		{
			try {
				if ( _wfcontroller.Workflow != null && !string.IsNullOrEmpty(_wfcontroller.Workflow.Path) &&
					 Directory.Exists(_wfcontroller.Workflow.Path) ) {
					dlgSelectFolder.SelectedPath = _wfcontroller.Workflow.Path;
					if ( !dlgSelectFolder.SelectedPath.EndsWith("\\") )
						dlgSelectFolder.SelectedPath += "\\";

				}
			} catch {
			}
			if ( dlgSelectFolder.ShowDialog() != DialogResult.OK )
				return;

			_wfcontroller.DeployVSProjectCSharp(dlgSelectFolder.SelectedPath, update);
		}
		#endregion

		#region EventHandlers

		private void FormBindingMapping_Load(object sender, EventArgs e)
		{
			displayWorkflowInfos();
			fillLogicalCapabilityList();
		}

		private void pnlItems_SizeChanged(object sender, EventArgs e)
		{
		}

		private void FormBindingMapping_SizeChanged(object sender, EventArgs e)
		{
			this.SuspendLayout();
			foreach ( Control ctrl in pnlItems.Controls ) {
				ctrl.Width = this.Width - 23;
			}
			this.ResumeLayout(true);
		}

		private void tsbtnDeploy_Click(object sender, EventArgs e)
		{
			doDeploy(true);
		}
		private void tsbtnRedeploy_Click(object sender, EventArgs e)
		{
			DialogResult result =
				MessageBox.Show("All external changes will be deleted." + Environment.NewLine + Environment.NewLine +
								"Do you want to continue?", tsbtnRedeploy.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
			if ( result == DialogResult.Yes ) {
				doDeploy(false);
			}
		}

		#endregion

		#endregion

	}
}